import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

public class DBTestControl {
	private DBTestFrame frame;
	private MySQLConnection connection;

	public DBTestControl() {
		try {
			connection = new MySQLConnection("root", "root", "MiviaMovieDB");

			EventQueue.invokeLater(new Runnable() {
				public void run() {
					try {
						frame = new DBTestFrame();

						frame.setQueries(QueryEnum.getAllEnum());
						frame.addQueryExecutorListener(new QueryExecuteListener());
						frame.setMovieTypesList(QueryMaker
								.getGenres(connection));

						frame.setVisible(true);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(null, "Connessione al DB fallita",
					"Errore di connessione", JOptionPane.ERROR_MESSAGE);
			e1.printStackTrace();
		}

	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		new DBTestControl();
	}

	private class QueryExecuteListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					QueryEnum query = frame.getSelectedQuery();
					String username = "";
					switch (query) {

					case GetDetailedMovie:
						try {
							int movieID = Integer.parseInt(frame.getMovieID());
							DetailedMovie detMovie = QueryMaker.getDetailedMovie(
									connection, movieID);
							if (detMovie != null)
								frame.showDetailedMovie(detMovie);
							else
								frame.setMessage("Film non trovato");
						} catch (NumberFormatException e1) {
							frame.setErrorOnParameters();
						} catch (SQLException e1) {
							e1.printStackTrace();
						}
						break;

					case GetGenres:
						try {
							List<MovieType> types = QueryMaker.getGenres(connection);
							frame.showMovieTypes(types);
						} catch (SQLException e1) {
							e1.printStackTrace();
						}
						break;

					case GetGenresByUser:
						String userId = frame.getUserID();
						if (!userId.equals("")) {
							List<MovieType> types;
							try {
								types = QueryMaker.getGenresByUser(connection, userId);
								frame.showUser(new User(userId, types));
							} catch (SQLException e1) {
								e1.printStackTrace();
							}
						} else {
							frame.setErrorOnParameters();
						}
						break;

					case GetMovies:
						List<Movie> movies;
						try {
							MovieFilter filter = frame.getMovieFilter();
							if (filter != null) {
								movies = QueryMaker.getMovies(connection, filter);
								frame.showMovies(movies);
							} else
								frame.setErrorOnParameters();
						} catch (SQLException e1) {
							e1.printStackTrace();
						}
						break;

					case GetMoviesByUser:
						String userID = frame.getUserID();
						int startIndex = frame.getStartIndex();
						int length = frame.getLength();

						if (!userID.equals("") && startIndex >= 0 && length >= 0) {
							try {
								frame.showMovies(QueryMaker.getMoviesByUser(connection, userID, startIndex,
										length));
							} catch (SQLException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
						
						}else
						{
							frame.setErrorOnParameters();
						}
						break;

					case GetMoviesCount:
						try {
							MovieFilter filter = frame.getMovieFilter();
							if (filter != null) {
								int count = QueryMaker.getMoviesCount(connection, filter);
								frame.showMessage("Movies count: " + count);
							} else
								frame.setErrorOnParameters();
						} catch (SQLException e1) {
							e1.printStackTrace();
						}
						break;

					case GetMoviesCountByUser:
						try {
							username = frame.getUserID();
							if (!username.equals("")) {
								int res = QueryMaker.getMoviesCountByUser(connection, username);
								frame.showMessage("Movies count: " + res);
							} else
								frame.setErrorOnParameters();
						} catch (SQLException e1) {
							e1.printStackTrace();
						}
						break;

					case GetNews:
						try {
							username = frame.getUserID();
							if (!username.equals("")) {
								List<Movie> news = QueryMaker.getNews(connection, username);
								frame.showMovies(news);
							} else
								frame.setErrorOnParameters();
						} catch (SQLException e1) {
							e1.printStackTrace();
						}
						
						break;

					case InsertUser:
						User user = frame.getUser();
						if(user!=null){
							try {
								QueryMaker.insertUser(connection, user);
								frame.setMessage("Utente "+user.getId()+" inserito correttamente");
							} catch (SQLException e1) {
								e1.printStackTrace();
							}
						}
						else{
							frame.setErrorOnParameters();
						}
						break;

					case IsUserRegistered:
						try {
							username = frame.getUserID();
							if (!username.equals("")) {
								boolean res = QueryMaker.isUserRegistered(connection, username);
								if(res)
									frame.showMessage("Utente "+username+" registrato");
								else
									frame.showMessage("Utente "+username+" non registrato");
							} else
								frame.setErrorOnParameters();
						} catch (SQLException e1) {
							e1.printStackTrace();
						}
						break;
						
					case UpdateUserGenres:
						User u = frame.getUser();
						if(u!=null){
							try {
								QueryMaker.updateUserGenres(connection, u);
								frame.setMessage("Utente "+u.getId()+" aggiornato correttamente");
							} catch (SQLException e1) {
								e1.printStackTrace();
							}
						}
						else{
							frame.setErrorOnParameters();
						}
						break;
					}
					
				}
			}).start();
		}

	}

}

enum QueryEnum {
	GetMovies, GetGenres, GetDetailedMovie, InsertUser, GetGenresByUser, UpdateUserGenres, GetNews, GetMoviesByUser, GetMoviesCountByUser, IsUserRegistered, GetMoviesCount;

	public static List<QueryEnum> getAllEnum() {
		List<QueryEnum> queries = new ArrayList<QueryEnum>();
		queries.add(GetMovies);
		queries.add(GetGenres);
		queries.add(GetDetailedMovie);
		queries.add(InsertUser);
		queries.add(GetGenresByUser);
		queries.add(UpdateUserGenres);
		queries.add(GetNews);
		queries.add(GetMoviesByUser);
		queries.add(GetMoviesCountByUser);
		queries.add(IsUserRegistered);
		queries.add(GetMoviesCount);

		return queries;
	}
}